package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description UserController
 * @Author Lwj
 * @Date 2024-09-03-15:29
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户分页
     *
     * @param userDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("page/{pageNum}/{pageSize}")
    @ApiOperation(value = "用户分页", notes = "用户分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userDto", value = "用户DTO对象", required = true, dataType = "UserDto"),
            @ApiImplicitParam(paramType = "path", name = "pageNum", value = "页码", example = "1", dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "pageSize", value = "每页条数", example = "10", dataType = "Integer")
    })
    @ApiOperationSupport(includeParameters = {"userDto.username", "userDto.email", "userDto.dataState", "userDto.deptNo", "pageNum", "pageSize"})
    public ResponseResult<PageResponse<UserVo>> getUserPage(
            @RequestBody UserDto userDto,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize) {
        log.info("用户分页!");
        PageResponse<UserVo> pageResponse = userService.getUserPage(userDto, pageNum, pageSize);
        return ResponseResult.success(pageResponse);
    }

    /**
     * 用户添加
     *
     * @param userDto
     * @return
     */
    @PutMapping
    @ApiOperation(value = "用户添加")
    public ResponseResult<Boolean> insertUser(@RequestBody UserDto userDto) {
        log.info("用户添加{}", userDto);
        Boolean b = userService.insertUser(userDto);
        return ResponseResult.success(b);
    }

    /**
     * 用户修改
     *
     * @param userDto
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "用户修改")
    public ResponseResult<Boolean> updateUser(@RequestBody UserDto userDto) {
        log.info("用户修改{}", userDto);
        Boolean flag = userService.updateUser(userDto);
        return ResponseResult.success(flag);
    }

    /**
     * 当前用户
     *
     * @return
     */
    /*@GetMapping("/current-user")
    @ApiOperation(value = "当前用户")
    public ResponseResult<UserVo> CurrentUser(@RequestBody UserDto userDto) {
        log.info("当前用户！");
        UserVo userVo = userService.CurrentUser(userDto);
        return ResponseResult.success(userVo);
    }*/

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @DeleteMapping("/remove/{userId}")
    @ApiOperation(value = "删除用户")
    public ResponseResult deleteUser(@PathVariable("userId") Long userId) {
        log.info("删除用户{}", userId);
        userService.deleteUser(userId);
        return ResponseResult.success();
    }

    /**
     * 启用或禁用用户
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation(value = "启用或禁用用户")
    public ResponseResult setStatus(@PathVariable("id") Long id, @PathVariable("status") String status) {
        log.info("启用或禁用用户{},{}", id, status);
        userService.setStatus(id, status);
        return ResponseResult.success();
    }

    /**
     * 用户列表
     *
     * @param userDto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "用户列表")
    public ResponseResult<UserVo> getUserList(@RequestBody UserDto userDto) {
        log.info("用户列表");
        UserVo userVo = userService.getUserList(userDto);
        return ResponseResult.success(userVo);
    }

    /**
     * 密码重置
     * @param userId
     * @return
     */
    @PostMapping("/reset-passwords/{userId}")
    @ApiOperation(value = "密码重置")
    public ResponseResult<Boolean> reSetPassword(@PathVariable("userId") String userId) {
        log.info("密码重置用户id为{}", userId);
        Boolean flag = userService.reSetPassword(userId);
        return ResponseResult.success(flag);
    }
}
